package com.sz.controller;

import com.sz.pojo.User;
import com.sz.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController{
    //controller调service
    @Autowired
    @Qualifier("UserServiceImpl")
    private UserService userService;

    //跳转到注册页面
    @RequestMapping("/register")
    public String toReg() {
        return "register";
    }

    //跳转到登录页面
    @RequestMapping("/index")
    public String toIndex(){
        return "redirect:/";
    }

    //跳转到最新专辑
    @RequestMapping("/latestalbum")
    public String toLatestalbum(){
        return "latestalbum";
    }

    //跳转到特色专辑
    @RequestMapping("/featuredalbum")
    public String toFeaturedalbum(){
        return "featuredalbum";
    }

    //跳转到加入我们
    @RequestMapping("/joinus")
    public String toJoinus(){
        return "joinus";
    }

    //跳转到上传音乐
    @RequestMapping("/portfolio")
    public String toPortfolio(){
        return "portfolio";
    }

    //跳转到团队
    @RequestMapping("/team")
    public String toTeam(){
        return "team";
    }

    //跳转到联系
    @RequestMapping("/contact")
    public String toContact(){
        return "contact";
    }

    //回到主页面
    @RequestMapping("/hope")
    public String toHopemusic(){
        return "hopemusic";
    }

    //登录功能
    @RequestMapping("/golog")
    public String log(User user, Model model, HttpSession session, HttpServletRequest request) {
        User u = userService.findOne(user);
        request.getSession().setAttribute("userName", u.getUserName());
        model.addAttribute("user",u);
        if (u != null) {
            return "hopemusic";
        } else
            return "logno";
    }

    //注册功能
    @RequestMapping("/goreg")
    public String reg(User user) {
        User u = userService.checkReg(user.getUserName());
        if (u == null) {
            userService.addOne(user);
            return "regok";
        }
        return "regno";
    }

    //查询全部的用户，并返回到用户展示页面
    @RequestMapping("/admin")
    public String list(Model model,HttpSession session){
        String userName =(String) session.getAttribute("userName");
        if (userName.equals("admin")) {
            List<User> list = userService.queryAllBook();
            model.addAttribute("list", list);
            return "allUser";
        }else {
            User user=userService.queryUserByName(userName);
            model.addAttribute("user",user);
            return "user";
        }
    }

    //跳转到增加页面
    @RequestMapping("/toAddUser")
    public String toAddPaper() {
        return "addUser";
    }
    //增加用户的操作
    @RequestMapping("/addUser")
    public String addPaper(User user) {
        //System.out.println(books);
        userService.addUser(user);
        return "redirect:/user/admin";
    }

    //跳转到修改用户页面
    @RequestMapping("/toUpdateUser")
    public String toUpdateBook(Model model, int id) {
        User user = userService.queryBookById(id);
        //System.out.println(books);
        model.addAttribute("user",user );
        return "updateUser";
    }

    //修改用户
    @RequestMapping("/updateUser")
    public String updateBook(Model model, User user) {
        //System.out.println(book);
        userService.updateUser(user);
        User users = userService.queryBookById(user.getId());
        model.addAttribute("users", users);
        return "redirect:/user/admin";
    }

    //删除用户
    @RequestMapping("/del/{id}")
    public String deleteBook(@PathVariable("id") int id){
        userService.deleteUserById(id);
        return "redirect:/user/admin";
    }

    //查询用户
    @RequestMapping("/queryUser")
    public String queryBookName(Model model,String userName){
        User user=userService.queryUserByName(userName);
        model.addAttribute("user",user);
        return "queryUser";
    }
}
